//
//  UIColor+HexColor.m
//  SleepingElf
//
//  Created by karong on 2023/6/6.

#import "UIColor+HexColor.h"

@implementation UIColor (HexColor)
#pragma mark - 工具方法

/*!
 * @method 通过16进制计算颜色
 * @abstract
 * @discussion
 * @param 16机制
 * @result 颜色对象
 */
+ (UIColor *)colorFromHexRGB:(NSString *)inColorString
{
    return [UIColor colorFromHexRGB:inColorString alpha:1.0];
}

+ (UIColor *)colorFromHexRGB:(NSString *)inColorString alpha:(CGFloat)alpha
{
    UIColor *result = nil;
    unsigned int colorCode = 0;
    unsigned char redByte, greenByte, blueByte;
    
    if (nil != inColorString)
    {
        NSScanner *scanner = [NSScanner scannerWithString:inColorString];
        (void) [scanner scanHexInt:&colorCode]; // ignore error
    }
    redByte = (unsigned char) (colorCode >> 16);
    greenByte = (unsigned char) (colorCode >> 8);
    blueByte = (unsigned char) (colorCode); // masks off high bits
    result = [UIColor
              colorWithRed: (float)redByte / 0xff
              green: (float)greenByte/ 0xff
              blue: (float)blueByte / 0xff
              alpha:alpha];
    return result;
}

#pragma mark - 通用颜色
+ (UIColor *)colorWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor {
    if (@available(iOS 13.0, *)) {
        return [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
                return darkColor;
            }
            else {
                return lightColor;
            }
        }];
    } else {
        return lightColor;
    }
}

+ (UIColor *)colorSameWithRGB:(NSString *)RGB {
    return [UIColor colorWithLightColor:[UIColor colorFromHexRGB:RGB] darkColor:[UIColor colorFromHexRGB:RGB alpha:0.16]];
}

+ (UIColor *)colorWithLightRGB:(NSString *)lightRGB darkRGB:(NSString *)darkRGB {
    return [UIColor colorWithLightColor:[UIColor colorFromHexRGB:lightRGB] darkColor:[UIColor colorFromHexRGB:darkRGB]];
}

+ (UIColor *)font_blue_8BA8F2 {
    return [UIColor colorWithLightColor:[UIColor colorFromHexRGB:@"8BA8F2"] darkColor:[UIColor colorFromHexRGB:@"8BA8F2"]];
}

+ (UIColor *)font_gray_999999 {
    return [UIColor colorWithLightColor:[UIColor colorFromHexRGB:@"999999"] darkColor:[UIColor colorFromHexRGB:@"999999"]];
}

+ (UIColor *)font_gray_727584 {
    return [UIColor colorWithLightColor:[UIColor colorFromHexRGB:@"727584"] darkColor:[UIColor colorFromHexRGB:@"727584"]];
}

+ (UIColor *)font_black_333333 {
    return [UIColor colorWithLightColor:[UIColor colorFromHexRGB:@"333333"] darkColor:[UIColor colorFromHexRGB:@"333333"]];
}

+ (UIColor *)bg_gray_f7f7f7 {
    return [UIColor colorWithLightColor:[UIColor colorFromHexRGB:@"f7f7f7"] darkColor:[UIColor colorFromHexRGB:@"f7f7f7"]];
}

+ (UIColor *)bg_gray_f2f2f2 {
    return [UIColor colorWithLightColor:[UIColor colorFromHexRGB:@"f2f2f2"] darkColor:[UIColor colorFromHexRGB:@"f2f2f2"]];
}

+ (UIColor *)mask_black_50p {
    return [UIColor colorWithLightColor:[UIColor colorFromHexRGB:@"000000" alpha:0.5] darkColor:[UIColor colorFromHexRGB:@"000000" alpha:0.5]];
}
@end
